package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Consumer;
import com.fc.campusapplicationsystem.service.ConsumerService;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 贺哲
 * @2020-02-29 15:28
 */
@RestController
@RequestMapping("/consumerController")
public class ConsumerController {

    @Autowired
    ConsumerService consumerService;

    @GetMapping("/list")
    public ResponseOBJ<List<Consumer>> list(@RequestParam("page") Integer page,
                                            @RequestParam("limit") Integer limit) {
        ResponseOBJ<List<Consumer>> responseOBJ = ResponseOBJ.getInstance();
        try {
            PageHelper.startPage(page, limit);
            List<Consumer> consumers = consumerService.list();
            PageInfo<Consumer> pageInfo = new PageInfo<>(consumers);
            responseOBJ.setData(consumers);
            responseOBJ.setCount(pageInfo.getTotal());
            responseOBJ.setMsg("查询成功");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setMsg("查询失败");
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody Consumer consumer) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (consumerService.checkUsername(consumer)){
                Integer result = consumerService.add(consumer);
                responseOBJ.setData(result);
                responseOBJ.setCode(ResponseOBJ.SUCCESS);
                responseOBJ.setMsg("注册成功");
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("注册失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("注册失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    @PostMapping("/update")
    public ResponseOBJ<Integer> update(@RequestBody Consumer consumer) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (consumerService.checkUsername(consumer)){
                Integer result = consumerService.update(consumer);
                responseOBJ.setData(result);
                responseOBJ.setCode(ResponseOBJ.SUCCESS);
                responseOBJ.setMsg("修改成功");
            }else {
                responseOBJ.setCode(ResponseOBJ.ERROR);
                responseOBJ.setMsg("修改失败,登录名已存在!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
            responseOBJ.setMsg("修改失败,请稍后再重试!");
        }
        return responseOBJ;
    }

    @GetMapping("/delete")
    public ResponseOBJ<Integer> delete(@RequestParam("consumerId") Integer id) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try {
            Integer result = consumerService.delete(id);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }
}
